#pragma once

#pragma managed

#include "ExplorerForm.h"
#include "../USISlave.h"
#include "../TreadStartStructs.h"
#include "../USIListSorter.h"
#include "InfoForm.h"
#include "SettingsForm.h"
#include "AboutForm.h"

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
 
namespace USI 
{

	public ref class MainForm : public System::Windows::Forms::Form
	{
	private:
		USISlave ^_slave;
		InfoForm ^infoForm;
		UInt32 expireDirs;
		Semaphore ^infoFormSemaphore;

		System::Void SetList(array<String ^> ^list)
		{
			pathsView->Items->Clear();
			
			for (int iItem = 0; iItem < list->Length; iItem++)
			{
				ListViewItem ^item = gcnew ListViewItem(list[iItem]);
				item->ImageIndex = 0;
				pathsView->Items->Add(item);
			}
		}

		array<String ^> ^GetList()
		{
			array<String ^> ^list = gcnew array<String ^>(pathsView->Items->Count);
			for (int iItem = 0; iItem < pathsView->Items->Count; iItem++)
			{
				list[iItem] = pathsView->Items[iItem]->Text;
			}

			return list;
		}

		delegate System::Void VoidDelegate();
		System::Void UpdateCombos()
		{
			if (this->InvokeRequired)
			{
				this->Invoke(gcnew VoidDelegate(this, &MainForm::UpdateCombos));
				return;
			}

			int iCompare = compareBox->SelectedIndex;
			int iCheck = checkComboBox->SelectedIndex;
			int iCheckWith = checkWithComboBox->SelectedIndex;

			compareBox->Items->Clear();
			checkComboBox->Items->Clear();
			checkWithComboBox->Items->Clear();

			array<String ^> ^shots = _slave->GetShots();
			if (shots->Length == 0)
			{
				compareBox->Enabled = false;
				checkComboBox->Enabled = false;
				checkWithComboBox->Enabled = false;
			}
			else
			{
				compareBox->Enabled = true;
				checkComboBox->Enabled = true;
				checkWithComboBox->Enabled = true;

				for (int iShot = 0; iShot < shots->Length; iShot++)
				{
					compareBox->Items->Add(shots[iShot]);
					checkComboBox->Items->Add(shots[iShot]);
					checkWithComboBox->Items->Add(shots[iShot]);
				}

				if (iCompare == -1)
					compareBox->SelectedIndex = compareBox->Items->Count - 1;
				else
					compareBox->SelectedIndex = iCompare;

				if (iCheck == -1)
					checkComboBox->SelectedIndex = checkComboBox->Items->Count - 1;
				else
					checkComboBox->SelectedIndex = iCheck;

				if (iCheckWith == -1)
					checkWithComboBox->SelectedIndex = checkWithComboBox->Items->Count - 1;
				else
					checkWithComboBox->SelectedIndex = iCheckWith;
			}
		}

		System::Void LoadShotsThread(Object ^_shots)
		{
			LoadShotsRec ^shots = dynamic_cast<LoadShotsRec ^>(_shots);
			_slave->LoadShot(shots->Shot1);
			_slave->LoadShot(shots->Shot2);

			infoFormSemaphore->WaitOne();
			infoForm->DoClose();
		}

		System::Void ShowLoadShotInfo()
		{
			if (this->InvokeRequired)
			{
				this->Invoke(gcnew VoidDelegate(this, &MainForm::ShowLoadShotInfo));
				return;
			}

			infoForm = gcnew InfoForm("Loading shot...", infoFormSemaphore);
			infoForm->ShowDialog();
		}

		System::Void ShowLoadShotInfoThread()
		{
			ShowLoadShotInfo();
		}

		System::Void MakeShotThread(Object ^_makeShots)
		{
			this->SetInterfaceEnabled(false);

			MakeShotRec ^makeShot = dynamic_cast<MakeShotRec ^>(_makeShots);
			_slave->MakeShot(makeShot->Paths);

			int progressPos = 0;

			do
			{
				int newPos = _slave->GetWork();
				if (newPos != progressPos)
				{
					progressPos = newPos;
					SetProgress(progressPos);
				}

				Thread::Sleep(500);
			}
			while(_slave->GetWip());

			UpdateCombos();

			if (makeShot->Shot != -1)
			{
				infoFormSemaphore = gcnew Semaphore(0, 1);
				Thread ^loadShotInfoThread = gcnew Thread(gcnew ThreadStart(this, &MainForm::ShowLoadShotInfoThread));
				loadShotInfoThread->Start();
				_slave->LoadShot(makeShot->Shot);
				infoFormSemaphore->WaitOne();
				infoForm->DoClose();

				_slave->CompareShots(makeShot->Shot, makeShot->BoxItemsCount);
				array<String ^, 2> ^res = _slave->GetResults();
				if (res != nullptr)
				{
					for (int iRes = 0; iRes < res->Length / 4; iRes++)
					{
						AddResult(res[iRes, 0], res[iRes, 1], res[iRes, 2], res[iRes, 3]);
					}
				}
			}

			this->SetInterfaceEnabled(true);
			SetProgress(0);
			_slave->ResetWork();
			MessageBox::Show("Finished!", "Success!", MessageBoxButtons::OK, MessageBoxIcon::Information);
		}

		System::Void ExpireDirsThread(Object ^_paths)
		{
			array<String ^> ^paths = dynamic_cast<array<String ^> ^>(_paths);
			expireDirs = _slave->ExpireDirs(paths);

			infoFormSemaphore->WaitOne();
			infoForm->DoClose();
		}

		System::Void CloseUSIThread(Object ^_list)
		{
			array<String ^> ^list = dynamic_cast<array<String ^> ^>(_list);
			_slave->SaveListDefault(list);
			_slave->Deinitialize();

			infoFormSemaphore->WaitOne();
			infoForm->DoClose();
		}

		delegate System::Void SetStartVisibleDelegate(bool visible);
		System::Void SetInterfaceEnabled(bool visible)
		{
			if (this->InvokeRequired)
			{
				this->Invoke(gcnew SetStartVisibleDelegate(this, &MainForm::SetInterfaceEnabled), gcnew array<Object ^> { visible });
				return;
			}

			startButton->Enabled = visible;
			shotCompareButton->Enabled = visible;
		}

		delegate void SetProgressDelegate(int value);
		System::Void SetProgress(int value)
		{
			if (this->InvokeRequired)
			{
				this->Invoke(gcnew SetProgressDelegate(this, &MainForm::SetProgress), gcnew array<Object ^> { value });
				return;
			}

			if (value < progressBar->Maximum)
				progressBar->Value = value;
		}

		delegate void AddResultDelegate(String ^rsn, String ^wrn, String ^path, String ^name);
		System::Void AddResult(String ^rsn, String ^wrn, String ^path, String ^name)
		{
			if (this->InvokeRequired)
			{
				this->Invoke(gcnew AddResultDelegate(this, &MainForm::AddResult), gcnew array<Object ^> { rsn, wrn, path, name });
				return;
			}

			ListViewItem ^item = gcnew ListViewItem(rsn);
			item->SubItems->Add(wrn);
			item->SubItems->Add(path);
			item->SubItems->Add(name);
			if (wrn->Equals("Danger!"))
				item->ImageIndex = 0;
			if (wrn->Equals("Warning"))
				item->ImageIndex = 1;
			if (wrn->Equals("Notification"))
				item->ImageIndex = 2;
			if (wrn->Equals("Information"))
				item->ImageIndex = 3;

			resultView->Items->Add(item);
		}


	public:
		MainForm(USISlave ^slave)
		{
			InitializeComponent();
			
			resultView->ListViewItemSorter = gcnew USIListSorter();
			_slave = slave;
			_slave->Initialize();

			UpdateCombos();

			SetList(_slave->LoadListDefault());
			progressBar->Visible = false;
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~MainForm()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::MenuStrip^  menuStrip;
	protected: 
	private: System::Windows::Forms::ToolStripMenuItem^  fileToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  selectBaseToolStripMenuItem;
	private: System::Windows::Forms::ToolStripSeparator^  toolStripMenuItem1;
	private: System::Windows::Forms::ToolStripMenuItem^  exitToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  settingsToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  settingsToolStripMenuItem1;
	private: System::Windows::Forms::ToolStripMenuItem^  helpToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  aboutToolStripMenuItem;
	private: System::Windows::Forms::TabControl^  tabControl;
	private: System::Windows::Forms::TabPage^  listPage;
	private: System::Windows::Forms::TabPage^  resultsPage;

	private: System::Windows::Forms::Panel^  panel1;
	private: System::Windows::Forms::ListView^  pathsView;


	private: System::Windows::Forms::Button^  saveButton;


	private: System::Windows::Forms::Button^  addButton;
	private: System::Windows::Forms::Button^  deleteButton;


	private: System::Windows::Forms::Button^  startButton;
	private: System::Windows::Forms::ComboBox^  compareBox;
	private: System::Windows::Forms::Button^  loadButton;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::ColumnHeader^  pathColumn;
	private: System::Windows::Forms::Panel^  panel2;
	private: System::Windows::Forms::ListView^  resultView;

	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::ProgressBar^  progressBar;
	private: System::Windows::Forms::ComboBox^  checkWithComboBox;



	private: System::Windows::Forms::ComboBox^  checkComboBox;
private: System::Windows::Forms::Button^  saveResultsButton;


private: System::Windows::Forms::Button^  shotCompareButton;


	private: System::Windows::Forms::ColumnHeader^  resultHeader;
	private: System::Windows::Forms::ColumnHeader^  changesHeader;
	private: System::Windows::Forms::ColumnHeader^  pathHeader;
	private: System::Windows::Forms::ColumnHeader^  nameHeader;
	private: System::Windows::Forms::ImageList^  pathsImageList;
private: System::Windows::Forms::ImageList^  resultsImageList;
	private: System::ComponentModel::IContainer^  components;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>


#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(MainForm::typeid));
			this->menuStrip = (gcnew System::Windows::Forms::MenuStrip());
			this->fileToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->selectBaseToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->toolStripMenuItem1 = (gcnew System::Windows::Forms::ToolStripSeparator());
			this->exitToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->settingsToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->settingsToolStripMenuItem1 = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->helpToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->aboutToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->tabControl = (gcnew System::Windows::Forms::TabControl());
			this->listPage = (gcnew System::Windows::Forms::TabPage());
			this->pathsView = (gcnew System::Windows::Forms::ListView());
			this->pathColumn = (gcnew System::Windows::Forms::ColumnHeader());
			this->pathsImageList = (gcnew System::Windows::Forms::ImageList(this->components));
			this->panel1 = (gcnew System::Windows::Forms::Panel());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->compareBox = (gcnew System::Windows::Forms::ComboBox());
			this->loadButton = (gcnew System::Windows::Forms::Button());
			this->saveButton = (gcnew System::Windows::Forms::Button());
			this->addButton = (gcnew System::Windows::Forms::Button());
			this->deleteButton = (gcnew System::Windows::Forms::Button());
			this->startButton = (gcnew System::Windows::Forms::Button());
			this->resultsPage = (gcnew System::Windows::Forms::TabPage());
			this->resultView = (gcnew System::Windows::Forms::ListView());
			this->resultHeader = (gcnew System::Windows::Forms::ColumnHeader());
			this->changesHeader = (gcnew System::Windows::Forms::ColumnHeader());
			this->pathHeader = (gcnew System::Windows::Forms::ColumnHeader());
			this->nameHeader = (gcnew System::Windows::Forms::ColumnHeader());
			this->resultsImageList = (gcnew System::Windows::Forms::ImageList(this->components));
			this->panel2 = (gcnew System::Windows::Forms::Panel());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->progressBar = (gcnew System::Windows::Forms::ProgressBar());
			this->checkWithComboBox = (gcnew System::Windows::Forms::ComboBox());
			this->checkComboBox = (gcnew System::Windows::Forms::ComboBox());
			this->saveResultsButton = (gcnew System::Windows::Forms::Button());
			this->shotCompareButton = (gcnew System::Windows::Forms::Button());
			this->menuStrip->SuspendLayout();
			this->tabControl->SuspendLayout();
			this->listPage->SuspendLayout();
			this->panel1->SuspendLayout();
			this->resultsPage->SuspendLayout();
			this->panel2->SuspendLayout();
			this->SuspendLayout();
			// 
			// menuStrip
			// 
			this->menuStrip->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(3) {this->fileToolStripMenuItem, 
				this->settingsToolStripMenuItem, this->helpToolStripMenuItem});
			this->menuStrip->Location = System::Drawing::Point(0, 0);
			this->menuStrip->Name = L"menuStrip";
			this->menuStrip->Size = System::Drawing::Size(779, 24);
			this->menuStrip->TabIndex = 0;
			this->menuStrip->Text = L"menuStrip1";
			// 
			// fileToolStripMenuItem
			// 
			this->fileToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(3) {this->selectBaseToolStripMenuItem, 
				this->toolStripMenuItem1, this->exitToolStripMenuItem});
			this->fileToolStripMenuItem->Name = L"fileToolStripMenuItem";
			this->fileToolStripMenuItem->Size = System::Drawing::Size(35, 20);
			this->fileToolStripMenuItem->Text = L"File";
			// 
			// selectBaseToolStripMenuItem
			// 
			this->selectBaseToolStripMenuItem->Name = L"selectBaseToolStripMenuItem";
			this->selectBaseToolStripMenuItem->Size = System::Drawing::Size(152, 22);
			this->selectBaseToolStripMenuItem->Text = L"Select base";
			this->selectBaseToolStripMenuItem->Click += gcnew System::EventHandler(this, &MainForm::selectBaseToolStripMenuItem_Click);
			// 
			// toolStripMenuItem1
			// 
			this->toolStripMenuItem1->Name = L"toolStripMenuItem1";
			this->toolStripMenuItem1->Size = System::Drawing::Size(149, 6);
			// 
			// exitToolStripMenuItem
			// 
			this->exitToolStripMenuItem->Name = L"exitToolStripMenuItem";
			this->exitToolStripMenuItem->Size = System::Drawing::Size(152, 22);
			this->exitToolStripMenuItem->Text = L"Exit";
			this->exitToolStripMenuItem->Click += gcnew System::EventHandler(this, &MainForm::exitToolStripMenuItem_Click);
			// 
			// settingsToolStripMenuItem
			// 
			this->settingsToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->settingsToolStripMenuItem1});
			this->settingsToolStripMenuItem->Name = L"settingsToolStripMenuItem";
			this->settingsToolStripMenuItem->Size = System::Drawing::Size(58, 20);
			this->settingsToolStripMenuItem->Text = L"Settings";
			// 
			// settingsToolStripMenuItem1
			// 
			this->settingsToolStripMenuItem1->Name = L"settingsToolStripMenuItem1";
			this->settingsToolStripMenuItem1->Size = System::Drawing::Size(124, 22);
			this->settingsToolStripMenuItem1->Text = L"Settings";
			this->settingsToolStripMenuItem1->Click += gcnew System::EventHandler(this, &MainForm::settingsToolStripMenuItem1_Click);
			// 
			// helpToolStripMenuItem
			// 
			this->helpToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->aboutToolStripMenuItem});
			this->helpToolStripMenuItem->Name = L"helpToolStripMenuItem";
			this->helpToolStripMenuItem->Size = System::Drawing::Size(40, 20);
			this->helpToolStripMenuItem->Text = L"Help";
			// 
			// aboutToolStripMenuItem
			// 
			this->aboutToolStripMenuItem->Name = L"aboutToolStripMenuItem";
			this->aboutToolStripMenuItem->Size = System::Drawing::Size(114, 22);
			this->aboutToolStripMenuItem->Text = L"About";
			this->aboutToolStripMenuItem->Click += gcnew System::EventHandler(this, &MainForm::aboutToolStripMenuItem_Click);
			// 
			// tabControl
			// 
			this->tabControl->Controls->Add(this->listPage);
			this->tabControl->Controls->Add(this->resultsPage);
			this->tabControl->Dock = System::Windows::Forms::DockStyle::Fill;
			this->tabControl->Location = System::Drawing::Point(0, 24);
			this->tabControl->Name = L"tabControl";
			this->tabControl->SelectedIndex = 0;
			this->tabControl->Size = System::Drawing::Size(779, 437);
			this->tabControl->TabIndex = 1;
			// 
			// listPage
			// 
			this->listPage->Controls->Add(this->pathsView);
			this->listPage->Controls->Add(this->panel1);
			this->listPage->Location = System::Drawing::Point(4, 22);
			this->listPage->Name = L"listPage";
			this->listPage->Padding = System::Windows::Forms::Padding(3);
			this->listPage->Size = System::Drawing::Size(771, 411);
			this->listPage->TabIndex = 0;
			this->listPage->Text = L"Check list";
			this->listPage->UseVisualStyleBackColor = true;
			// 
			// pathsView
			// 
			this->pathsView->Columns->AddRange(gcnew cli::array< System::Windows::Forms::ColumnHeader^  >(1) {this->pathColumn});
			this->pathsView->Dock = System::Windows::Forms::DockStyle::Fill;
			this->pathsView->Location = System::Drawing::Point(3, 3);
			this->pathsView->Name = L"pathsView";
			this->pathsView->Size = System::Drawing::Size(765, 343);
			this->pathsView->SmallImageList = this->pathsImageList;
			this->pathsView->TabIndex = 0;
			this->pathsView->UseCompatibleStateImageBehavior = false;
			this->pathsView->View = System::Windows::Forms::View::Details;
			// 
			// pathColumn
			// 
			this->pathColumn->Text = L"Path";
			this->pathColumn->Width = 560;
			// 
			// pathsImageList
			// 
			this->pathsImageList->ImageStream = (cli::safe_cast<System::Windows::Forms::ImageListStreamer^  >(resources->GetObject(L"pathsImageList.ImageStream")));
			this->pathsImageList->TransparentColor = System::Drawing::Color::Fuchsia;
			this->pathsImageList->Images->SetKeyName(0, L"XPfolder_closed.bmp");
			// 
			// panel1
			// 
			this->panel1->BackColor = System::Drawing::Color::Transparent;
			this->panel1->Controls->Add(this->label1);
			this->panel1->Controls->Add(this->compareBox);
			this->panel1->Controls->Add(this->loadButton);
			this->panel1->Controls->Add(this->saveButton);
			this->panel1->Controls->Add(this->addButton);
			this->panel1->Controls->Add(this->deleteButton);
			this->panel1->Controls->Add(this->startButton);
			this->panel1->Dock = System::Windows::Forms::DockStyle::Bottom;
			this->panel1->Location = System::Drawing::Point(3, 346);
			this->panel1->Name = L"panel1";
			this->panel1->Size = System::Drawing::Size(765, 62);
			this->panel1->TabIndex = 2;
			// 
			// label1
			// 
			this->label1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
			this->label1->AutoSize = true;
			this->label1->Font = (gcnew System::Drawing::Font(L"Tahoma", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->label1->Location = System::Drawing::Point(357, 13);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(102, 16);
			this->label1->TabIndex = 6;
			this->label1->Text = L"Compare with:";
			// 
			// compareBox
			// 
			this->compareBox->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
			this->compareBox->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->compareBox->FormattingEnabled = true;
			this->compareBox->Location = System::Drawing::Point(360, 35);
			this->compareBox->Name = L"compareBox";
			this->compareBox->Size = System::Drawing::Size(157, 21);
			this->compareBox->TabIndex = 5;
			// 
			// loadButton
			// 
			this->loadButton->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
			this->loadButton->Location = System::Drawing::Point(523, 6);
			this->loadButton->Name = L"loadButton";
			this->loadButton->Size = System::Drawing::Size(75, 23);
			this->loadButton->TabIndex = 4;
			this->loadButton->Text = L"Load list";
			this->loadButton->UseVisualStyleBackColor = true;
			this->loadButton->Click += gcnew System::EventHandler(this, &MainForm::loadButton_Click);
			// 
			// saveButton
			// 
			this->saveButton->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
			this->saveButton->Location = System::Drawing::Point(523, 34);
			this->saveButton->Name = L"saveButton";
			this->saveButton->Size = System::Drawing::Size(75, 23);
			this->saveButton->TabIndex = 3;
			this->saveButton->Text = L"Save list";
			this->saveButton->UseVisualStyleBackColor = true;
			this->saveButton->Click += gcnew System::EventHandler(this, &MainForm::saveButton_Click);
			// 
			// addButton
			// 
			this->addButton->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
			this->addButton->Location = System::Drawing::Point(604, 6);
			this->addButton->Name = L"addButton";
			this->addButton->Size = System::Drawing::Size(75, 23);
			this->addButton->TabIndex = 2;
			this->addButton->Text = L"Add";
			this->addButton->UseVisualStyleBackColor = true;
			this->addButton->Click += gcnew System::EventHandler(this, &MainForm::addButton_Click);
			// 
			// deleteButton
			// 
			this->deleteButton->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
			this->deleteButton->Location = System::Drawing::Point(604, 34);
			this->deleteButton->Name = L"deleteButton";
			this->deleteButton->Size = System::Drawing::Size(75, 23);
			this->deleteButton->TabIndex = 1;
			this->deleteButton->Text = L"Delete";
			this->deleteButton->UseVisualStyleBackColor = true;
			this->deleteButton->Click += gcnew System::EventHandler(this, &MainForm::deleteButton_Click);
			// 
			// startButton
			// 
			this->startButton->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
			this->startButton->Location = System::Drawing::Point(685, 6);
			this->startButton->Name = L"startButton";
			this->startButton->Size = System::Drawing::Size(75, 51);
			this->startButton->TabIndex = 0;
			this->startButton->Text = L"Start";
			this->startButton->UseVisualStyleBackColor = true;
			this->startButton->Click += gcnew System::EventHandler(this, &MainForm::startButton_Click);
			// 
			// resultsPage
			// 
			this->resultsPage->Controls->Add(this->resultView);
			this->resultsPage->Controls->Add(this->panel2);
			this->resultsPage->Location = System::Drawing::Point(4, 22);
			this->resultsPage->Name = L"resultsPage";
			this->resultsPage->Padding = System::Windows::Forms::Padding(3);
			this->resultsPage->Size = System::Drawing::Size(771, 411);
			this->resultsPage->TabIndex = 1;
			this->resultsPage->Text = L"Check results";
			this->resultsPage->UseVisualStyleBackColor = true;
			// 
			// resultView
			// 
			this->resultView->Columns->AddRange(gcnew cli::array< System::Windows::Forms::ColumnHeader^  >(4) {this->resultHeader, this->changesHeader, 
				this->pathHeader, this->nameHeader});
			this->resultView->Dock = System::Windows::Forms::DockStyle::Fill;
			this->resultView->Location = System::Drawing::Point(3, 3);
			this->resultView->Name = L"resultView";
			this->resultView->Size = System::Drawing::Size(765, 343);
			this->resultView->SmallImageList = this->resultsImageList;
			this->resultView->Sorting = System::Windows::Forms::SortOrder::Ascending;
			this->resultView->TabIndex = 1;
			this->resultView->UseCompatibleStateImageBehavior = false;
			this->resultView->View = System::Windows::Forms::View::Details;
			// 
			// resultHeader
			// 
			this->resultHeader->Text = L"Result";
			this->resultHeader->Width = 120;
			// 
			// changesHeader
			// 
			this->changesHeader->Text = L"Changes";
			this->changesHeader->Width = 120;
			// 
			// pathHeader
			// 
			this->pathHeader->Text = L"Path";
			this->pathHeader->Width = 360;
			// 
			// nameHeader
			// 
			this->nameHeader->Text = L"Name";
			this->nameHeader->Width = 160;
			// 
			// resultsImageList
			// 
			this->resultsImageList->ImageStream = (cli::safe_cast<System::Windows::Forms::ImageListStreamer^  >(resources->GetObject(L"resultsImageList.ImageStream")));
			this->resultsImageList->TransparentColor = System::Drawing::Color::Transparent;
			this->resultsImageList->Images->SetKeyName(0, L"DangerIcon.bmp");
			this->resultsImageList->Images->SetKeyName(1, L"WarningIcon.bmp");
			this->resultsImageList->Images->SetKeyName(2, L"NotifyIcon.bmp");
			this->resultsImageList->Images->SetKeyName(3, L"InfoIcon.bmp");
			// 
			// panel2
			// 
			this->panel2->Controls->Add(this->label3);
			this->panel2->Controls->Add(this->label2);
			this->panel2->Controls->Add(this->progressBar);
			this->panel2->Controls->Add(this->checkWithComboBox);
			this->panel2->Controls->Add(this->checkComboBox);
			this->panel2->Controls->Add(this->saveResultsButton);
			this->panel2->Controls->Add(this->shotCompareButton);
			this->panel2->Dock = System::Windows::Forms::DockStyle::Bottom;
			this->panel2->Location = System::Drawing::Point(3, 346);
			this->panel2->Name = L"panel2";
			this->panel2->Size = System::Drawing::Size(765, 62);
			this->panel2->TabIndex = 0;
			// 
			// label3
			// 
			this->label3->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
			this->label3->AutoSize = true;
			this->label3->Font = (gcnew System::Drawing::Font(L"Tahoma", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->label3->Location = System::Drawing::Point(410, 37);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(43, 16);
			this->label3->TabIndex = 6;
			this->label3->Text = L"With:";
			// 
			// label2
			// 
			this->label2->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
			this->label2->AutoSize = true;
			this->label2->Font = (gcnew System::Drawing::Font(L"Tahoma", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(204)));
			this->label2->Location = System::Drawing::Point(410, 9);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(70, 16);
			this->label2->TabIndex = 5;
			this->label2->Text = L"Compare:";
			// 
			// progressBar
			// 
			this->progressBar->Anchor = static_cast<System::Windows::Forms::AnchorStyles>(((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left) 
				| System::Windows::Forms::AnchorStyles::Right));
			this->progressBar->Location = System::Drawing::Point(5, 20);
			this->progressBar->Name = L"progressBar";
			this->progressBar->Size = System::Drawing::Size(399, 23);
			this->progressBar->TabIndex = 4;
			// 
			// checkWithComboBox
			// 
			this->checkWithComboBox->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
			this->checkWithComboBox->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->checkWithComboBox->FormattingEnabled = true;
			this->checkWithComboBox->Location = System::Drawing::Point(486, 35);
			this->checkWithComboBox->Name = L"checkWithComboBox";
			this->checkWithComboBox->Size = System::Drawing::Size(137, 21);
			this->checkWithComboBox->TabIndex = 3;
			// 
			// checkComboBox
			// 
			this->checkComboBox->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
			this->checkComboBox->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->checkComboBox->FormattingEnabled = true;
			this->checkComboBox->Location = System::Drawing::Point(486, 7);
			this->checkComboBox->Name = L"checkComboBox";
			this->checkComboBox->Size = System::Drawing::Size(137, 21);
			this->checkComboBox->TabIndex = 2;
			// 
			// saveResultsButton
			// 
			this->saveResultsButton->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
			this->saveResultsButton->Location = System::Drawing::Point(629, 6);
			this->saveResultsButton->Name = L"saveResultsButton";
			this->saveResultsButton->Size = System::Drawing::Size(131, 23);
			this->saveResultsButton->TabIndex = 1;
			this->saveResultsButton->Text = L"Save results";
			this->saveResultsButton->UseVisualStyleBackColor = true;
			// 
			// shotCompareButton
			// 
			this->shotCompareButton->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
			this->shotCompareButton->Location = System::Drawing::Point(629, 34);
			this->shotCompareButton->Name = L"shotCompareButton";
			this->shotCompareButton->Size = System::Drawing::Size(131, 23);
			this->shotCompareButton->TabIndex = 0;
			this->shotCompareButton->Text = L"Compare snapshots";
			this->shotCompareButton->UseVisualStyleBackColor = true;
			this->shotCompareButton->Click += gcnew System::EventHandler(this, &MainForm::shotCompareButton_Click);
			// 
			// MainForm
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(779, 461);
			this->Controls->Add(this->tabControl);
			this->Controls->Add(this->menuStrip);
			this->Icon = (cli::safe_cast<System::Drawing::Icon^  >(resources->GetObject(L"$this.Icon")));
			this->MainMenuStrip = this->menuStrip;
			this->Name = L"MainForm";
			this->ShowInTaskbar = false;
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"USI";
			this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &MainForm::MainForm_FormClosing);
			this->menuStrip->ResumeLayout(false);
			this->menuStrip->PerformLayout();
			this->tabControl->ResumeLayout(false);
			this->listPage->ResumeLayout(false);
			this->panel1->ResumeLayout(false);
			this->panel1->PerformLayout();
			this->resultsPage->ResumeLayout(false);
			this->panel2->ResumeLayout(false);
			this->panel2->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

		private: System::Void addButton_Click(System::Object^  sender, System::EventArgs^  e) 
		{
			ExplorerForm ^form = gcnew ExplorerForm(_slave, _ExploreType::Directories, _slave->GetWorkDir());
			if (form->ShowDialog() == ::DialogResult::OK)
			{
				array<String ^> ^dirs = form->Files;
				for (int iDir = 0; iDir < dirs->Length; iDir++)
				{
					ListViewItem ^item = gcnew ListViewItem(dirs[iDir]);
					item->ImageIndex = 0;
					pathsView->Items->Add(item);
				}
			}
		}

		private: System::Void deleteButton_Click(System::Object^  sender, System::EventArgs^  e) 
		{
			int count = pathsView->SelectedItems->Count;

			for (int iItem = 0; iItem < count; iItem++)
			{
				pathsView->Items->Remove(pathsView->SelectedItems[0]);
			}
		}

		private: System::Void MainForm_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e)
		{
			infoFormSemaphore = gcnew Semaphore(0, 1);
			infoForm = gcnew InfoForm("Saving shots...", infoFormSemaphore);
			Thread ^edThread = gcnew Thread(gcnew ParameterizedThreadStart(this, &MainForm::CloseUSIThread));
			edThread->Start(GetList());
			infoForm->ShowDialog();
		}

		private: System::Void loadButton_Click(System::Object^  sender, System::EventArgs^  e)
		{
			ExplorerForm ^form = gcnew ExplorerForm(_slave, _ExploreType::Lists, _slave->GetWorkDir());
			if (form->ShowDialog() == ::DialogResult::OK)
			{
				array<String ^> ^file = form->Files;
				if (file->Length > 0)
				{
					SetList(_slave->LoadList(file[0]));
				}
			}
		}

		private: System::Void saveButton_Click(System::Object^  sender, System::EventArgs^  e)
		{
			ExplorerForm ^form = gcnew ExplorerForm(_slave, _ExploreType::Lists, _slave->GetWorkDir(), true);
			if (form->ShowDialog() == ::DialogResult::OK)
			{
				String ^file = form->SaveFile;
				if (!Path::GetExtension(file)->Equals(".list"))
				{
					file = file + ".list";
				}
				_slave->SaveList(GetList(), file);
			}
		}

		private: System::Void selectBaseToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e)
		{
			ExplorerForm ^form = gcnew ExplorerForm(_slave, _ExploreType::Databases, _slave->GetWorkDir(), true);
			if (form->ShowDialog() == ::DialogResult::OK)
			{
				String ^file = form->SaveFile;
				if (!Path::GetExtension(file)->Equals(".db"))
				{
					file = file + ".db";
				}
				_slave->SelectDB(file);
			}
		}


		private: System::Void startButton_Click(System::Object^  sender, System::EventArgs^  e)
		{
			array<String ^> ^paths = GetList();
			
			infoFormSemaphore = gcnew Semaphore(0, 1);
			infoForm = gcnew InfoForm("Estimating...", infoFormSemaphore);
			Thread ^edThread = gcnew Thread(gcnew ParameterizedThreadStart(this, &MainForm::ExpireDirsThread));
			edThread->Start(paths);
			infoForm->ShowDialog();

			progressBar->Maximum = expireDirs * _slave->GetPointWeight();
			progressBar->Value = 0;
			progressBar->Visible = true;
			resultView->Items->Clear();

			tabControl->SelectedIndex = 1;

			Thread ^thread = gcnew Thread(gcnew ParameterizedThreadStart(this, &MainForm::MakeShotThread));
			thread->Start(gcnew MakeShotRec(paths, compareBox->SelectedIndex, compareBox->Items->Count));
		}

		private: System::Void shotCompareButton_Click(System::Object^  sender, System::EventArgs^  e)
		{
			if (checkComboBox->SelectedIndex == -1 || checkWithComboBox->SelectedIndex == -1)
				return;

			if (checkComboBox->SelectedIndex == checkWithComboBox->SelectedIndex)
			{
				MessageBox::Show("Please, select two different snapshots!", "Warning!", MessageBoxButtons::OK, MessageBoxIcon::Warning);
				return;
			}

			resultView->Items->Clear();
			progressBar->Visible = true;
			progressBar->Value = 0;
			progressBar->Maximum = 1;
			resultView->Items->Clear();

			LoadShotsRec ^rec = gcnew LoadShotsRec(checkComboBox->SelectedIndex, checkWithComboBox->SelectedIndex);
			infoFormSemaphore = gcnew Semaphore(0, 1);
			infoForm = gcnew InfoForm("Loading shots...", infoFormSemaphore);
			Thread ^loadThread = gcnew Thread(gcnew ParameterizedThreadStart(this, &MainForm::LoadShotsThread));
			loadThread->Start(rec);
			infoForm->ShowDialog();

			_slave->CompareShots(checkComboBox->SelectedIndex, checkWithComboBox->SelectedIndex);

			array<String ^, 2> ^res = _slave->GetResults();
			if (res != nullptr)
			{
				for (int iRes = 0; iRes < res->Length / 4; iRes++)
				{
					AddResult(res[iRes, 0], res[iRes, 1], res[iRes, 2], res[iRes, 3]);
				}
			}
			_slave->ResetWork();
			progressBar->Value = 1;
			MessageBox::Show("Finished!", "Success!", MessageBoxButtons::OK, MessageBoxIcon::Information);
		}

		private: System::Void settingsToolStripMenuItem1_Click(System::Object^  sender, System::EventArgs^  e)
		{
			SettingsForm ^form = gcnew SettingsForm(_slave);
			form->ShowDialog();
		}

		private: System::Void aboutToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e)
		{
			AboutForm ^form = gcnew AboutForm();
			form->ShowDialog();
		}

		private: System::Void exitToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e)
		{
			Close();
		}
};
}
